package com.yonyou.pmclouds.procedureinspect.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.annotation.Insert;
import com.yonyou.pmclouds.basecom.entity.AdditionalPropertiesVOWrapper;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.service.CommonPropertiesProcessor;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.constructionlog.entity.ConstructionLogRefVO;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.processinspect.entity.ProcedureInspectBodyVO;
import com.yonyou.pmclouds.processinspect.entity.ProcedureInspectVO;
import com.yonyou.pmclouds.processinspect.entity.ProcedureRecordRefVO;
import com.yonyou.pmclouds.processinspect.rmiitf.ProcedureInspectRemoteService;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value="/mobile/procedureinspect")
public class ProcedureInspectController {

	@Reference(version = ApplicationConsts.APPLICATION_VERSION)
	private ProcedureInspectRemoteService procedureInspectService;
	@Autowired
	private UserInfoQuery userInfoQuery;
    @Autowired
    private CommonPropertiesProcessor propertiesProcessor;

	@Insert
	@RequestMapping(value="/insertProcedureInspect")
	@ResponseBody
	@PMApprove
	public ProcedureInspectVO insertProcedureInspect(@RequestBody ProcedureInspectVO procedureInspectVO) throws BusinessException{

		checkFieldValue(procedureInspectVO);

		//智能日志需要制单人信息
		String creatorName = userInfoQuery.queryUserName(RuntimeEnvironment.getLoginUserId());
		procedureInspectVO.setCreatorname(creatorName);

		procedureInspectVO = procedureInspectService.insertProcedureInspect(procedureInspectVO);

		userInfoQuery.tranUserInfoField(new ProcedureInspectVO[] {procedureInspectVO}, new String[] {CommonFieldConst.CREATOR},
				new String[] {ProcedureInspectVO.CREATORNAME});

		return procedureInspectVO;
	}
	/**
     *
	 * 方法功能描述：校验必输字段。
     *
	 * <b>参数说明</b>
	 * @param procedureInspectVO
	 * @throws BusinessException
	 * @author zhangwce
	 * @time 2017年11月15日 下午3:12:08
	 */
	private void checkFieldValue(ProcedureInspectVO procedureInspectVO) throws BusinessException{

		if(procedureInspectVO.getPk_procedure() == null){
			throw new BusinessException("工序为空，无法保存！");
		}
		if(procedureInspectVO.getPk_project() == null){
			throw new BusinessException("项目为空，无法保存！");
		}
		if(procedureInspectVO.getPk_records() == null || procedureInspectVO.getPk_records().length == 0){
			throw new BusinessException("未选择施工记录，无法报验！");
		}

    }

	@RequestMapping(value="/queryDetailById")
	@ResponseBody
	public ProcedureInspectVO queryDetailById(String pk_procedure_inspect) throws BusinessException{

		ProcedureInspectVO procedureInspectVO = procedureInspectService.queryPrcInspectDetail(pk_procedure_inspect);

		userInfoQuery.tranUserInfoField(new ProcedureInspectVO[]{procedureInspectVO},
				new String[]{ProcedureInspectVO.CREATOR,ProcedureInspectVO.ACCEPTOR},
				new String[]{ProcedureInspectVO.CREATORNAME,ProcedureInspectVO.ACCEPTORNAME});

        userInfoQuery.tranUserInfoField(procedureInspectVO.getBodyVOs(),
				new String[]{ProcedureInspectBodyVO.RECORD_CREATOR},
				new String[]{ProcedureInspectBodyVO.RECORD_CREATOR_NAME});


        return procedureInspectVO;
	}

    @RequestMapping(value="/queryPrcInspectByPrj")
	@ResponseBody
    public AdditionalPropertiesVOWrapper[] queryPrcInspectByPrj(String pk_project) throws BusinessException {
        List<ProcedureInspectVO> procedureInspectList = procedureInspectService.queryPrcInspectByPrj(pk_project);
        if (procedureInspectList == null) {
            return null;
        }
        ProcedureInspectVO[] beans = procedureInspectList.toArray(new ProcedureInspectVO[0]);
        userInfoQuery.tranUserInfoField(beans,
                new String[]{ProcedureInspectVO.CREATOR, ProcedureInspectVO.ACCEPTOR},
                new String[]{ProcedureInspectVO.CREATORNAME, ProcedureInspectVO.ACCEPTORNAME});
        return propertiesProcessor.wrapAdditionalProps(beans);
    }

//	@RequestMapping(value="/approveProcedureInpect")
//	@ResponseBody
//	public ProcedureInspectVO approveProcedureInpect(@RequestBody ProcedureInspectVO procedureInspectVO) throws BusinessException{
//
//        String[] updateFields = new String[]{ProcedureInspectVO.APPROVE_OPINION,ProcedureInspectVO.BILLSTATUS};
//
//        return procedureInspectService.approveProcedureInspect(procedureInspectVO, updateFields);
//	}

    @RequestMapping(value="/queryRecordForPrcInspect")
	@ResponseBody
	public List<ProcedureRecordRefVO> queryRecordForPrcInspect(String pk_project) throws BusinessException{

        // 获得施工记录参照
		List<ConstructionLogRefVO> recordRefList = procedureInspectService.queryRecordForPrcInspect(pk_project);
		if(recordRefList == null || recordRefList.size() == 0){
			return new ArrayList<ProcedureRecordRefVO>();
		}

        userInfoQuery.tranUserInfoField(recordRefList.toArray(new ConstructionLogRefVO[0]),
				new String[]{ConstructionLogRefVO.CREATOR},
				new String[]{ConstructionLogRefVO.CREATORNAME});

        List<ProcedureRecordRefVO> prcRecordRefList = getProcedureRecordRefList(recordRefList);

        return prcRecordRefList;

    }
	/**
     *
	 * 方法功能描述：组装结果集。
     *
	 * <b>参数说明</b>
	 * @param recordRefList
	 * @return
	 * @author zhangwce
	 * @time 2017年11月29日 下午2:39:26
	 */
	private List<ProcedureRecordRefVO> getProcedureRecordRefList(List<ConstructionLogRefVO> recordRefList) {

        Map<String,List<ConstructionLogRefVO>> procedureRecordMap = new HashMap<String, List<ConstructionLogRefVO>>();

        String tempProcess = null;
		for(ConstructionLogRefVO refVO : recordRefList){
			tempProcess = refVO.getPk_process();
			if(procedureRecordMap.get(tempProcess) == null)
				procedureRecordMap.put(tempProcess, new ArrayList<ConstructionLogRefVO>());
			procedureRecordMap.get(tempProcess).add(refVO);
		}

        List<ProcedureRecordRefVO> prcRecordRefList = new ArrayList<ProcedureRecordRefVO>();

        for (String pk_process : procedureRecordMap.keySet()) {
			ProcedureRecordRefVO prcRecordRefVO = new ProcedureRecordRefVO();
			prcRecordRefVO.setPk_procedure(pk_process);
			prcRecordRefVO.setProcedure_name(procedureRecordMap.get(pk_process).get(0).getProcess_name());
			prcRecordRefVO.setRecordList(procedureRecordMap.get(pk_process));

            prcRecordRefList.add(prcRecordRefVO);
		}
		return prcRecordRefList;
	}

}
